package com.ny.controller;
import com.ny.entity.Classrooms;
import com.ny.entity.Reservations;
import com.ny.entity.Users;

import com.ny.service.ClassroomService;
import com.ny.service.ReservationService;
import com.ny.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ClassroomService classroomService;
    @Autowired
    private ReservationService reservationService;
    @PostMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, Model model, HttpSession session) {
        Users user = userService.login(username, password);
//        存储用户信息到session中
        session.setAttribute("user", user);

        if (user != null) {
            List<Classrooms> classroomsList = classroomService.findAllClassrooms();
//            classroomsList:代表查询出来的数据。
//            "classroomsList"  model的名字。jsp 就是从中拿取数据。
//            查询出来的数据存放到model中，然后jsp 从model中取出来。jsp就可以获取到数据库查询出来的信息了
            model.addAttribute("classroomsList", classroomsList);
//            model.addAttribute("user", user);
            return "classroom";
        } else {
            model.addAttribute("error", "用户名或密码错误");
            return "login";
        }
    }

    // http:localhost:8082/
// 多条件筛选:  提交的路径:/user/searchClassrooms   2.提交的方法 get  3.请求数据：
    @RequestMapping("/searchClassrooms")
    public String searchClassrooms(@RequestParam String classroomName,
                                   @RequestParam String capacity,
                                   @RequestParam String status,
                                   @RequestParam String building,
                                   @RequestParam String description,
                                   Model model) {

        List<Classrooms> classroomsList = classroomService.searchClassrooms(classroomName, capacity, status, building, description);
        model.addAttribute("classroomsList", classroomsList);
        return "classroom";
    }

    @RequestMapping("/prosionalcenter")
    public String prosionalcenter() {
        return "/personalCenter";
    }

    @RequestMapping("/findReservation")
    public String findReservation() {
        return "/myReservations";
    }
    @RequestMapping("/mychangePassword")
    public String changePassword() {
        return "/changePassword";
    }
    @RequestMapping("/changePhoneNumber")
    public String changePhoneNumber() {
        return "/changePhoneNumber";
    }
    @RequestMapping("/updateUsersPassword")
    public String updateUsersPassword(@RequestParam int userId, @RequestParam String password, Model model) {
        userService.updateUsersPassword(userId, password);
        model.addAttribute("success", "密码修改成功");
        return "/changePassword";
    }
    @RequestMapping("/changePhoneNumber1")
    public String changePhoneNumber(@RequestParam int userId, @RequestParam String phoneNumber, Model model) {
         userService.updatePhoneNumber(userId, phoneNumber);
         model.addAttribute("success", "手机号修改成功");
        return "/changePhoneNumber";
    }
    @RequestMapping("/findReservationsByUserId")
    public String findReservationsByUserId( Model model,HttpSession session) {
        Users user = (Users) session.getAttribute("user");
        List<Reservations> reservationsList = userService.findReservationsByUserId(user.getUserId());
        model.addAttribute("reservationsList", reservationsList);
        return "/myReservations";
    }
    @GetMapping("/searchReservations")
    public String getReservations(@RequestParam(required = false) String username, @RequestParam(required = false) String classroomName, @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm") Date startDatetime, @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm") Date endDatetime, @RequestParam(required = false) String status, Model model) {
        List<Reservations> reservationsList = userService.findReservations(username, classroomName, startDatetime, endDatetime, status);
        model.addAttribute("reservationList", reservationsList);
        return "reservations";
    }
}
